package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要删除的图书名字：");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.next();

        int curSize=bookList.getUsedSize();
        //处理特殊情况，如果删除的恰好是最后一本书
        if(bookList.getBooks(curSize-1).getName().equals(name))
        {
            bookList.setBooks(null,curSize-1);
            bookList.setUsedSize(curSize-1);
            System.out.println("删除图书成功");
            return;
        }
        int i = 0;
        for (; i < curSize-1; i++) {
            if(bookList.getBooks(i).getName().equals(name)){
                for (int i1 = i; i1 < curSize-1; i1++) {
                    bookList.setBooks(bookList.getBooks(i1+1), i1);
                }
                bookList.setBooks(null,curSize-1);
                bookList.setUsedSize(curSize-1);
                System.out.println("删除图书成功");
                return;
            }
        }
        if(i==curSize-1)//for循环后i==curSize-1
            System.out.println("无此书，将自动返回最初页面");
        work(bookList);
    }
}
